package mop.gui.task;

import java.awt.GridLayout;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import mop.controller.CreateTaskController;
import mop.gui.GUI;
import mop.gui.component.SpringUtilities;
import mop.model.World;
import mop.model.task.TaskInterface;
import mop.model.task.field.FieldInterface;
import mop.model.task.tasktype.TaskTypeInterface;

/**
 * Dialog for custom task fields
 * 
 * @author  MOP Team 7
 */
public class CreateTaskDialog extends javax.swing.JDialog {

    /**
     * Constructor.
     *
     * @param   gui
     * @param   createTaskController
     * @param   taskType
     */
    public CreateTaskDialog(GUI gui, CreateTaskController createTaskController, TaskTypeInterface taskType)
    {
        super(gui.getMainWindow(), true);
        initComponents();
        setGui(gui);

        // Set controller
        this.createTaskController = createTaskController;

        // Set the task type
        this.taskType = taskType;
        this.lblTaskType.setText("(Task Type: " + taskType.getDescription() + ")");

        // Load start & deadline in correct format.
        setStart(World.getTime());
        setDeadline(World.getTime());

        // Load fiels
        this.fields = taskType.getFieldList();

        // Fill custom panel
        //this.pnlCustom.setLayout(new GridLayout(this.fields.size(), 2));
        this.pnlCustom.setLayout(new SpringLayout());
        this.inputs = new LinkedList<JTextField>();
        for (FieldInterface f : this.fields)
        {
            JLabel label = new JLabel(f.getDescription());
            this.pnlCustom.add(label);
            JTextField textfield = new JTextField(10);
            textfield.setHorizontalAlignment(JTextField.LEFT);
            this.inputs.add(textfield);
            this.pnlCustom.add(textfield);
        }
        SpringUtilities.makeCompactGrid(this.pnlCustom,
            this.fields.size(), 2, //rows, cols
            5, 5, //initialX, initialY
            5, 5);//xPad, yPad

        // Load dependencies
        loadTasks();
    }

    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/

    private GUI gui;

    /**
     * Sets the GUI.
     *
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }

    /**
     * Returns the GUI reference.
     *
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }

    /***************************************************************************
     *                          Task Type
     ***************************************************************************/

    private TaskTypeInterface taskType;

    /***************************************************************************
     *                          Fields
     ***************************************************************************/

    private List<FieldInterface> fields;
    private List<JTextField> inputs;

    /***************************************************************************
     *                          Dialog-specific functionality
     ***************************************************************************/

    // keep track of checkboxes
    private LinkedList<JCheckBox> taskBoxes;

    /**
     * Loads the list of tasks on which the new task can depend.
     */
    private void loadTasks()
    {
        LinkedList<TaskInterface> tasks = (LinkedList<TaskInterface>) createTaskController.getPossibleDependencies();

        // load panel with checkboxes
        this.taskBoxes = new LinkedList<JCheckBox>();
        pnlTasks.setLayout(new GridLayout(tasks.size(), 1));
        for(TaskInterface t : tasks)
        {
            this.taskBoxes.add(new JCheckBox(t.getId()));
            pnlTasks.add(this.taskBoxes.getLast());
        }
    }

    private DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);

    /**
     * Validate and set the datetime field on the screen given a datetime string.
     * @param dateTime The datetime string
     */
    public void setStart(String dateTimeString)
    {
        this.setStart(this.getDateTime(dateTimeString));
    }

    /**
     * Validate and set the datetime field on the screen given a date.
     * @param dateTime The datetime object
     */
    public void setStart(Date dateTime)
    {
        String dateTimeString = "";
        if (dateTime != null)
            dateTimeString = dateTimeFormat.format(dateTime);
        txtStartTime.setText(dateTimeString);
        btnStartDate.setTargetDate(dateTime);
        btnStartTime.setTargetDate(dateTime);
    }

    /**
     * Returns the start time as a Date object.
     *
     * @return  start time as Date
     */
    public Date getStart()
    {
        return this.getDateTime(txtStartTime.getText());
    }

    /**
     * Validate and set the datetime field on the screen given a datetime string.
     * @param dateTime The datetime string
     */
    public void setDeadline(String dateTimeString)
    {
        this.setDeadline(this.getDateTime(dateTimeString));
    }

    /**
     * Validate and set the datetime field on the screen given a date.
     * @param dateTime The datetime object
     */
    public void setDeadline(Date dateTime)
    {
        String dateTimeString = "";
        if (dateTime != null)
            dateTimeString = dateTimeFormat.format(dateTime);
        txtDeadline.setText(dateTimeString);
        btnDeadlineDate.setTargetDate(dateTime);
        btnDeadlineTime.setTargetDate(dateTime);
    }

    /**
     * Returns the deadline as a Date object.
     *
     * @return  deadline as Date
     */
    public Date getDeadline()
    {
        return this.getDateTime(txtDeadline.getText());
    }

    /**
     * Returns a given date time string as a Date object.
     *
     * @return  Date object
     */
    public Date getDateTime(String dateTimeString)
    {
        Date dateTime = null;
        try
        {
            if ((dateTimeString != null) && (dateTimeString.length() > 0))
                dateTime = dateTimeFormat.parse(dateTimeString);
	}
        catch (Exception e)
        {
            dateTime = null;
        }
        return dateTime;
    }

    /***************************************************************************
     *                          Controllers
     ***************************************************************************/

    private CreateTaskController createTaskController;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTaskType = new javax.swing.JLabel();
        btnCreate = new javax.swing.JButton();
        lblCreateTask = new javax.swing.JLabel();
        lblId = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        btnCancel = new javax.swing.JButton();
        pnlDateTime = new javax.swing.JPanel();
        lblStartTime = new javax.swing.JLabel();
        txtStartTime = new javax.swing.JTextField();
        btnStartTime = new net.sourceforge.jcalendarbutton.JTimeButton();
        btnStartDate = new org.sourceforge.jcalendarbutton.JCalendarButton();
        lblDeadline = new javax.swing.JLabel();
        txtDeadline = new javax.swing.JTextField();
        btnDeadlineTime = new net.sourceforge.jcalendarbutton.JTimeButton();
        btnDeadlineDate = new org.sourceforge.jcalendarbutton.JCalendarButton();
        pnlDuration = new javax.swing.JPanel();
        lblDurationWeeks = new javax.swing.JLabel();
        txtDurationWeeks = new javax.swing.JTextField();
        lblDurationDays = new javax.swing.JLabel();
        txtDurationDays = new javax.swing.JTextField();
        lblDurationHours = new javax.swing.JLabel();
        txtDurationHours = new javax.swing.JTextField();
        lblDurationMinutes = new javax.swing.JLabel();
        txtDurationMinutes = new javax.swing.JTextField();
        pnlDep = new javax.swing.JPanel();
        lblDepTasks = new javax.swing.JLabel();
        scrlTasks = new javax.swing.JScrollPane();
        pnlTasks = new javax.swing.JPanel();
        pnlCCustom = new javax.swing.JPanel();
        lblCustomFields = new javax.swing.JLabel();
        scrlCustom = new javax.swing.JScrollPane();
        pnlCustom = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Create a Task");

        lblTaskType.setText("(Task Type: none)");
        lblTaskType.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        btnCreate.setText("Create Task");
        btnCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateActionPerformed(evt);
            }
        });

        lblCreateTask.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblCreateTask.setText("Create Task");

        lblId.setText("Task Id");

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        lblStartTime.setText("Start Time");

        txtStartTime.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtStartTimefocusStart(evt);
            }
        });

        btnStartTime.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                btnStartTimechangeStart(evt);
            }
        });

        btnStartDate.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                btnStartDatechangeStart(evt);
            }
        });

        lblDeadline.setText("Deadline");

        txtDeadline.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDeadlinefocusDeadline(evt);
            }
        });

        btnDeadlineTime.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                btnDeadlineTimechangeDeadline(evt);
            }
        });

        btnDeadlineDate.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                btnDeadlineDatechangeDeadline(evt);
            }
        });

        javax.swing.GroupLayout pnlDateTimeLayout = new javax.swing.GroupLayout(pnlDateTime);
        pnlDateTime.setLayout(pnlDateTimeLayout);
        pnlDateTimeLayout.setHorizontalGroup(
            pnlDateTimeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDateTimeLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDateTimeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblStartTime)
                    .addComponent(lblDeadline)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDateTimeLayout.createSequentialGroup()
                        .addGroup(pnlDateTimeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtDeadline, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                            .addComponent(txtStartTime, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlDateTimeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDateTimeLayout.createSequentialGroup()
                                .addComponent(btnStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnStartTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDateTimeLayout.createSequentialGroup()
                                .addComponent(btnDeadlineDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnDeadlineTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        pnlDateTimeLayout.setVerticalGroup(
            pnlDateTimeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDateTimeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblStartTime)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDateTimeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnStartTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtStartTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDeadline)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDateTimeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnDeadlineTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDeadlineDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDeadline, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblDurationWeeks.setText("Duration - Weeks");

        txtDurationWeeks.setText("0");

        lblDurationDays.setText("Duration - Days");

        txtDurationDays.setText("0");

        lblDurationHours.setText("Duration - Hours");

        txtDurationHours.setText("0");

        lblDurationMinutes.setText("Duration - Minutes");

        txtDurationMinutes.setText("0");

        javax.swing.GroupLayout pnlDurationLayout = new javax.swing.GroupLayout(pnlDuration);
        pnlDuration.setLayout(pnlDurationLayout);
        pnlDurationLayout.setHorizontalGroup(
            pnlDurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDurationLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtDurationDays, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtDurationWeeks, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                    .addComponent(lblDurationWeeks, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDurationDays, javax.swing.GroupLayout.Alignment.LEADING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDurationLayout.createSequentialGroup()
                        .addComponent(lblDurationHours)
                        .addGap(47, 47, 47))
                    .addGroup(pnlDurationLayout.createSequentialGroup()
                        .addComponent(lblDurationMinutes)
                        .addContainerGap(38, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDurationLayout.createSequentialGroup()
                        .addGroup(pnlDurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtDurationMinutes, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
                            .addComponent(txtDurationHours, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        pnlDurationLayout.setVerticalGroup(
            pnlDurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDurationLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDurationWeeks)
                    .addComponent(lblDurationHours))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDurationWeeks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDurationHours, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDurationDays)
                    .addComponent(lblDurationMinutes))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDurationDays, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDurationMinutes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblDepTasks.setText("Depends on tasks...");

        javax.swing.GroupLayout pnlTasksLayout = new javax.swing.GroupLayout(pnlTasks);
        pnlTasks.setLayout(pnlTasksLayout);
        pnlTasksLayout.setHorizontalGroup(
            pnlTasksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 231, Short.MAX_VALUE)
        );
        pnlTasksLayout.setVerticalGroup(
            pnlTasksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        scrlTasks.setViewportView(pnlTasks);

        javax.swing.GroupLayout pnlDepLayout = new javax.swing.GroupLayout(pnlDep);
        pnlDep.setLayout(pnlDepLayout);
        pnlDepLayout.setHorizontalGroup(
            pnlDepLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDepLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDepLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrlTasks, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                    .addComponent(lblDepTasks))
                .addContainerGap())
        );
        pnlDepLayout.setVerticalGroup(
            pnlDepLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDepLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblDepTasks)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrlTasks, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                .addContainerGap())
        );

        lblCustomFields.setText("Custom Fields");

        javax.swing.GroupLayout pnlCustomLayout = new javax.swing.GroupLayout(pnlCustom);
        pnlCustom.setLayout(pnlCustomLayout);
        pnlCustomLayout.setHorizontalGroup(
            pnlCustomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 251, Short.MAX_VALUE)
        );
        pnlCustomLayout.setVerticalGroup(
            pnlCustomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        scrlCustom.setViewportView(pnlCustom);

        javax.swing.GroupLayout pnlCCustomLayout = new javax.swing.GroupLayout(pnlCCustom);
        pnlCCustom.setLayout(pnlCCustomLayout);
        pnlCCustomLayout.setHorizontalGroup(
            pnlCCustomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCCustomLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCCustomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrlCustom, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                    .addComponent(lblCustomFields))
                .addContainerGap())
        );
        pnlCCustomLayout.setVerticalGroup(
            pnlCCustomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCCustomLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCustomFields)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrlCustom, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblCreateTask)
                                .addGap(18, 18, 18)
                                .addComponent(lblTaskType))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(lblId))))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(btnCreate, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addGap(20, 20, 20)
                                    .addComponent(txtId))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addComponent(pnlDateTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(pnlCCustom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(pnlDep, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(pnlDuration, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addGap(31, 31, 31))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCreateTask)
                    .addComponent(lblTaskType))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblId)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlDateTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlDuration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlDep, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlCCustom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCreate)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateActionPerformed
        try {
            // Get id
            String id = this.txtId.getText();

            // Parse start and end time/dates.
            Timestamp start = null;
            Timestamp deadline = null;
            try {
                start = new Timestamp(this.getStart().getTime());
                deadline = new Timestamp(this.getDeadline().getTime());
            } catch (Throwable th) {
                gui.handleError("There's something wrong with the start and/or deadline date & time!");
                return;
            }

            // Load duration.
            long ms = 0;
            ms += Long.parseLong(this.txtDurationMinutes.getText()) * 60000; // 1 minute = 60k ms
            ms += Long.parseLong(this.txtDurationHours.getText()) * 3600000; // 1 hour = 3.6m ms
            ms += Long.parseLong(this.txtDurationDays.getText()) * 86400000; // 1 day = 86.4m ms
            ms += Long.parseLong(this.txtDurationWeeks.getText()) * 604800000; // 1 week = 604.8m ms
            Timestamp duration = new Timestamp(ms);

            // Load dependant tasks.
            LinkedList<String> depTasks = new LinkedList<String>();
            for ( JCheckBox box : this.taskBoxes )
                if ( box.isSelected() )
                    depTasks.add(box.getText());

            // Check custom field inputs
            HashMap<String,String> fieldInputs = new HashMap<String,String>();
            int i = 0;
            for (JTextField tf : this.inputs)
            {
                FieldInterface field = fields.get(i++);
                String input = tf.getText();

                // @TODO Ook checken of Field van IntField or StringField is.
                if (input == null || input.equals(""))
                {
                    gui.handleError("You left some fields blank!");
                    return;
                }

                fieldInputs.put(field.getId(), input);
            }
            
            TaskInterface result = createTaskController.createTask(id, this.taskType, fieldInputs, start, duration, deadline, depTasks);
            gui.getMainWindow().update();
            gui.handleSuccess("Task created!");
            (new AssignTaskDialog(gui, createTaskController, result)).setVisible(true);
            this.dispose();
        } catch (Throwable exception) {
            gui.handleError(exception);
        }
    }//GEN-LAST:event_btnCreateActionPerformed

    private void btnDeadlineDatechangeDeadline(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_btnDeadlineDatechangeDeadline
        if (evt.getNewValue() instanceof Date)
            setDeadline((Date)evt.getNewValue());
}//GEN-LAST:event_btnDeadlineDatechangeDeadline

    private void btnDeadlineTimechangeDeadline(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_btnDeadlineTimechangeDeadline
        if (evt.getNewValue() instanceof Date)
            setDeadline((Date)evt.getNewValue());
}//GEN-LAST:event_btnDeadlineTimechangeDeadline

    private void btnStartTimechangeStart(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_btnStartTimechangeStart
        if (evt.getNewValue() instanceof Date)
            setStart((Date)evt.getNewValue());
}//GEN-LAST:event_btnStartTimechangeStart

    private void btnStartDatechangeStart(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_btnStartDatechangeStart
        if (evt.getNewValue() instanceof Date)
            setStart((Date)evt.getNewValue());
}//GEN-LAST:event_btnStartDatechangeStart

    private void txtStartTimefocusStart(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtStartTimefocusStart
        setStart(txtStartTime.getText());
}//GEN-LAST:event_txtStartTimefocusStart

    private void txtDeadlinefocusDeadline(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDeadlinefocusDeadline
        setDeadline(txtDeadline.getText());
}//GEN-LAST:event_txtDeadlinefocusDeadline

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCreate;
    private org.sourceforge.jcalendarbutton.JCalendarButton btnDeadlineDate;
    private net.sourceforge.jcalendarbutton.JTimeButton btnDeadlineTime;
    private org.sourceforge.jcalendarbutton.JCalendarButton btnStartDate;
    private net.sourceforge.jcalendarbutton.JTimeButton btnStartTime;
    private javax.swing.JLabel lblCreateTask;
    private javax.swing.JLabel lblCustomFields;
    private javax.swing.JLabel lblDeadline;
    private javax.swing.JLabel lblDepTasks;
    private javax.swing.JLabel lblDurationDays;
    private javax.swing.JLabel lblDurationHours;
    private javax.swing.JLabel lblDurationMinutes;
    private javax.swing.JLabel lblDurationWeeks;
    private javax.swing.JLabel lblId;
    private javax.swing.JLabel lblStartTime;
    private javax.swing.JLabel lblTaskType;
    private javax.swing.JPanel pnlCCustom;
    private javax.swing.JPanel pnlCustom;
    private javax.swing.JPanel pnlDateTime;
    private javax.swing.JPanel pnlDep;
    private javax.swing.JPanel pnlDuration;
    private javax.swing.JPanel pnlTasks;
    private javax.swing.JScrollPane scrlCustom;
    private javax.swing.JScrollPane scrlTasks;
    private javax.swing.JTextField txtDeadline;
    private javax.swing.JTextField txtDurationDays;
    private javax.swing.JTextField txtDurationHours;
    private javax.swing.JTextField txtDurationMinutes;
    private javax.swing.JTextField txtDurationWeeks;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtStartTime;
    // End of variables declaration//GEN-END:variables

}